import {requestUrl} from "./config.js"
export default function(url, data = {}, method = "POST",header = {}) {
	return new Promise((success, fail) => {
		header['content-type'] = 'application/x-www-form-urlencoded';
		header['token']        = uni.getStorageSync('token');
		uni.showLoading({
			title:'加载中……'
		})
		uni.request({
			url: requestUrl + url,
			data: data,
			method: method,
			header: header,
			success: res => {
				uni.hideLoading();
				if(res.data.code == 200 || res.data.code == 201){
					success(res.data);
				}else if(res.data.code == 400 || res.data.code == 401){
					uni.showToast({
					    title: res.data.msg,
					    duration: 2000,
						icon : 'none',
						success:function(){
							setTimeout(function(){
								uni.redirectTo({
									url: '/pages/auth/auth',
									success: res => {},
									fail: () => {},
									complete: () => {}
								});
							},2000)
						}
					});
					
				}else{
					uni.showToast({
					    title: res.data.msg,
					    duration: 2000,
						icon : 'none'
					});
				}
			},
			fail: err => {				
				uni.hideLoading();
				uni.showToast({
				    title: err.errMsg,
				    duration: 2000,
					icon : 'none'
				});
				fail(err);
			},
			complete: () => {}
			
		})
	});
};
/**
module.exports = {
 data
}
**/